package com.mfangsoft.zhuangjialong.integration.login.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.code.kaptcha.Producer;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.HttpClientUtil;
import com.mfangsoft.zhuangjialong.common.utils.MD5Util;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.common.utils.UserSessionUtil;
import com.mfangsoft.zhuangjialong.integration.enums.UserState;
import com.mfangsoft.zhuangjialong.integration.permission.model.PermissionEntity;
import com.mfangsoft.zhuangjialong.integration.permission.service.PermissionService;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.model.UserInfoEntity;
import com.mfangsoft.zhuangjialong.integration.user.service.UserService;
import com.mongodb.util.JSON;
@Controller
public class LoginController {
	
	
	  @Autowired  
      private Producer captchaProducer = null;   
	  
	  @Autowired
	  
	  private UserService userService;
	  
	  
	  @Autowired
	  private  PermissionService permissionService;
	  
	  
	  private final static boolean IS_V_CODE= true;
	  
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public void  login(UserEntity user,HttpSession session,HttpServletResponse response,HttpServletRequest request) throws IOException{
		
				
		UserEntity  userEntity=userService.getUserByAccount2(user.getAccount());
		
		if(userEntity!=null){
			
		    
			UserEntity  sessionUserEntity=userService.getUserById(userEntity.getId());
			
//			session.setAttribute(UserContext.USER_PERMISSION, userService.getUserPermissionByUserId(userEntity.getId()));

			session.setAttribute(UserContext.CURRENT_USER_INFO, userService.getUserById(sessionUserEntity.getId()));

			
			session.setAttribute(UserContext.USER_PERMISSION, userService.getUserPermissionByUserId(sessionUserEntity));
			
			UserSessionUtil.setUserSession(session);
//			response.sendRedirect("views/dashboard/index.html");
			response.sendRedirect("pages/index/index.html");
		}else{
			
			response.sendRedirect("login");
		}
	}
	
	
	@RequestMapping(value="/v_user",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String>  validateUser (UserEntity user,HttpSession session,HttpServletResponse response) throws IOException{
		
		ResponseMessage<String> message = new ResponseMessage<String>();
		
		if(IS_V_CODE){
			
			
			if(session.getAttribute(SysConstant.KAPTCHA_SESSION_KEY)==null){
				
				 message.setCode(SysConstant.FAILURE_CODE);
				 message.setMessage("重新获取验证码");
				 
				 return  message;
			}
			
			if(user.getV_code().equals(session.getAttribute(SysConstant.KAPTCHA_SESSION_KEY).toString())){
				 return this.v_user(user, message);
			 }else{
				 
				 message.setCode(SysConstant.FAILURE_CODE);
				 message.setMessage("验证码错误");
				 
				 return  message;
			 }
		}else{
			
			return this.v_user(user, message);
		}
		

		
	}
	
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	
	@CrossOrigin
	public void login2(HttpSession session,HttpServletResponse response) throws IOException{
		
		
		response.sendRedirect("login.html");
		
		
	}
@RequestMapping(value="/logindata",method=RequestMethod.GET)
	
	@CrossOrigin
	public void logindata(HttpSession session,HttpServletResponse response) throws IOException{
		
		
		//数据分析页面，可以删除
		response.sendRedirect("dataAnalysis.html");
		
		
	}
	private  ResponseMessage<String>   v_user(UserEntity userEntity, ResponseMessage<String> message){
		if(userEntity.getType()!=null && userEntity.getType()==1){
			Map sellerZZBMResult = this.getSellerZZBMResult(userEntity.getOrganization_code(), userEntity.getDepartment_code());
			if(sellerZZBMResult.get("data") == null){
				message.setCode("1");
				message.setMessage("部门号不正确！");
				return message;
			}else{
				UserEntity entity=userService.getUserByAccount(userEntity.getAccount());
				if(entity==null){
					
					message.setCode(SysConstant.FAILURE_CODE);
					message.setMessage("账号不存在");
					
					return message;
				}
				
				if(entity.getState().intValue()==UserState.CLOSE.getIndex().intValue()){
					
					message.setCode(SysConstant.FAILURE_CODE);
					message.setMessage("账号已关闭");
					return message;
				}
				
				
				if(entity.getPwd().equals(MD5Util.MD5(userEntity.getPwd()))){
					
					
					message.setCode(SysConstant.SUCCESS_CODE);
					message.setMessage(SysConstant.SUCCESS_MSG);
					
					return  message;
					
				}else{
					
					message.setCode(SysConstant.FAILURE_CODE);
					message.setMessage("密码错误");
					
					return message;
				}
			}
		}else{
			UserEntity entity=userService.getUserByAccount1(userEntity.getAccount());
			if(entity==null){
				
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("账号不存在");
				
				return message;
			}
			
			if(entity.getState().intValue()==UserState.CLOSE.getIndex().intValue()){
				
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("账号已关闭");
				return message;
			}
			
			
			if(entity.getPwd().equals(MD5Util.MD5(userEntity.getPwd()))){
				
				
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				
				return  message;
				
			}else{
				
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage("密码错误");
				
				return message;
			}
		}
		
		
	}
	
	@RequestMapping(value="/getvcode",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> getVcode(HttpServletResponse response,HttpSession session)
	{
		 
        String code = (String)session.getAttribute(SysConstant.KAPTCHA_SESSION_KEY);  
        response.setDateHeader("Expires", 0);  
          
        // Set standard HTTP/1.1 no-cache headers.  
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");  
          
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).  
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");  
          
        // Set standard HTTP/1.0 no-cache header.  
        response.setHeader("Pragma", "no-cache");  
          
        // return a jpeg  
        response.setContentType("image/jpeg");  
          
        // create the text for the image  
        String capText = captchaProducer.createText();  
          
        // store the text in the session  
        session.setAttribute(SysConstant.KAPTCHA_SESSION_KEY, capText);  
          
        // create the image with the text  
        BufferedImage bi = captchaProducer.createImage(capText);  
        
        ByteArrayOutputStream  arrayOutputStream = new ByteArrayOutputStream();
        
        
        // write the data out  
        try {
			ImageIO.write(bi, "jpg", arrayOutputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        
        byte []  b=arrayOutputStream.toByteArray();
        
        String imageStr=Base64Utils.encodeToString(b);
        ResponseMessage<String>  message=new ResponseMessage<String>();
        
        message.setCode(SysConstant.SUCCESS_CODE);
        message.setMessage(SysConstant.SUCCESS_MSG);
        
        message.setResult(imageStr);
        
        return message;
       
	}
	@RequestMapping(value="/server/getPermissionByUserId",method=RequestMethod.GET)
	@ResponseBody
	public List<PermissionEntity> getPermissionByUserId(HttpSession httpSession){
	
		return UserContext.getCurrentPermission();
		
		
		
	}
	
	
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<String> logout(HttpSession session){
		
		
		if (session.getAttribute(SysConstant.USER_INFO) != null) {

			session.removeAttribute(SysConstant.USER_INFO);
		}

		if (session.getAttribute(UserContext.CURRENT_USER_INFO) != null) {

			session.removeAttribute(UserContext.CURRENT_USER_INFO);
		}

		if (session.getAttribute(UserContext.PARENT_USER_INFO) != null) {

			session.removeAttribute(UserContext.PARENT_USER_INFO);
		}
		if (session.getAttribute(UserContext.USER_PERMISSION) != null) {

			session.removeAttribute(UserContext.USER_PERMISSION);
		}

		UserSessionUtil.remove();

		ResponseMessage<String> message = new ResponseMessage<>();

		message.setCode(SysConstant.SUCCESS_CODE);
		message.setMessage(SysConstant.SUCCESS_MSG);
		return message;
		
	}
	
	
	@RequestMapping(value="/server/getCurrentUser",method=RequestMethod.GET)
	@ResponseBody
	public Object getCurrentUser(HttpSession session){
		
		
		return UserContext.getCurrentUser();
		
	}
	@RequestMapping(value="/server/getCurrentUserInfo",method=RequestMethod.GET)
	@ResponseBody
	public UserInfoEntity getCurrentUserInfo(HttpSession session){
		
		
		return UserContext.getCurrentUserInfo();
		
	}
	
	public Map getSellerZZBMResult(String orgNo,String depNo) {
		try {
			URI uri = new URI("http://mos.powerdekor.com.cn:8011/qobinf.ashx?type=GOI03&sn=qobi01.powerdekor.com.cn&orgNo="+ orgNo +"&depNo=" + depNo);
			String sellerLoginJson = HttpClientUtil.sendGetForJson(uri);
			System.out.println(sellerLoginJson);
			Map map = (Map)JSON.parse(sellerLoginJson);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap();
		}
	}
}
